package model
{
	import mx.collections.ArrayCollection;
	import org.flashIso.engine.base.IsoPerspective;
	
	import org.flashIso.engine.base.IsoRoom;

	public class ModelRooms
	{
		public const defaultRoomName:String = "Untitled Room";

		[Bindable]
		public var roomsDataProvider:ArrayCollection = new ArrayCollection();

		[Bindable]
		private var selectedTiles:ArrayCollection = new ArrayCollection();
		[Bindable]
		private var selectedObjects:ArrayCollection = new ArrayCollection();
		[Bindable]
		public var currentPerspective:IsoPerspective;
		
		public function clear() : void {
			roomsDataProvider.removeAll();
		}
		
		public function addRoom(room:IsoRoom) : void {
			if (getRoomById(room.id) != null){
				var x:uint = 2;
				while (getRoomById(room.id + ' ' + x) != null){
					x++;
				}
				room.id = room.id + ' ' + x;
			}
			roomsDataProvider.addItemAt(room, 0);
			
		}
		public function removeRoom(room:IsoRoom ) : Boolean {
			roomsDataProvider.removeItemAt(roomsDataProvider.getItemIndex(room));
			return true;
		}
		public function getRoomById(id:String) : IsoRoom {
			var i :uint;
			for (i = 0; i < roomsDataProvider.length; i++){
				if ((roomsDataProvider[i] as IsoRoom).id == id){
					return roomsDataProvider[i];
				}
			}
			return null;
		}
		
		public function toXML() : XML {
			var xml:XML = <Rooms />;
			for (var i:uint = 0; i < roomsDataProvider.length; i++ ) {
				xml.appendChild((roomsDataProvider[i] as IsoRoom).toXML());
			}
			return xml;
		}
	}
}